/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class UnixOSStatusChecker implements RunStatusChecker {
	public static final Log logger=LogFactory.getLog(UnixOSStatusChecker.class);
    //auto finish if the job has not been finished in 3 days.
    public static long AUTO_FINISHTIME = 3 * 24 * 3600*1000;

    public void checkStatus(UnfinishedJob status) {
        File file = new File(status.getWorkdir() + File.separator +
                UinxOSSubmiter.FINISH_MARK);

        if (!file.exists()) {
            File scriptfile = new File(status.getWorkdir() + File.separator +
                    UinxOSSubmiter.SCRIPT_FILE);
            logger.info(status.getId()+" finish mark file does not exist");
            if (!scriptfile.exists()) {
                //deleted somehow
                status.setCode(RunStatusCode.UNKNOWN);
                logger.info(status.getId()+" can not find script file for "+status.getId()+" status set to unknown");
                status.setNote("can not find script file.");
                status.setFinishedTime(new Date());
            } else {
                if ((System.currentTimeMillis() -
                        status.getSubmittedTime().getTime()) > AUTO_FINISHTIME) {
                	 logger.info("job "+status.getId()+" status set to finished since the script file modification time is over expiration time");
                    status.setCode(RunStatusCode.FINISHED);
                    status.setNote("job is set to finish state fince it has not finished in preset waiting time");
                    status.setFinishedTime(new Date());
                } else {
                    status.setCode(RunStatusCode.RUNNING);
                }
            }

            return;
        }

        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = reader.readLine();
            status.setCode(RunStatusCode.FINISHED);
            status.setNote(status.getId()+" finished judged from finish mark file" );
            status.setExitValue(Integer.parseInt(line));
            logger.info(status.getId()+" successfully finished by parsing finish mark file");
        } catch (IOException e) {
            status.setCode(RunStatusCode.FINISHED);
            status.setExitValue(-1);
            logger.info(status.getId()+" finished but error when reading finish mark file"+e.getMessage());
        }

        status.setFinishedTime(new Date(file.lastModified()));
    }
    
    public void postJobProcess(UnfinishedJob job)
    {
    	
    }
}
